"""
Copyright (c) 2024 Beijing Jiaotong University
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Yunzhou Tang
Created: 2024/02/14
Supported by: National Key Research and Development Program of China
"""

import phot

if __name__ == "__main__":
    """ 本代码为程序主函数 本代码主要适用于 QPSK,16QAM,32QAM,64QAM 调制格式的单载波相干背靠背(B2B)信号 """

    phot.config(plot=True, backend="torch")  # 配置基本信息 开启画图 使用torch作为运算库后端

    """ 设置全局系统仿真参数 """
    num_symbols = 2**16                              # 符号数目
    bits_per_symbol = 4                              # 2 for QPSK; 4 for 16QAM; 6 for 64QAM  设置调制格式
    total_baud = 40e9                                # 信号波特率，符号率
    up_sampling_factor = 4                           # 上采样倍数
    sampling_rate = up_sampling_factor * total_baud  # 信号采样率
    Reference_frequency = 193.1e12
    
    """ 发射端 """
    """ RRC """
    RRC_ROLL_OFF = 0.01

    """ DAC """
    DAC_Resolution_Bits = 7
    DAC_OUTPUT_VPP = 1

    """ 光源 """
    Linewidth = 1e3
    output_power = 20

    """ 调制器 """
    Extinction_ratio = 65
    VPI = 5
    VDC = -2.5

    """ OSNR """
    osnr = 32

    signals = phot.transmitter(num_symbols, bits_per_symbol, total_baud, up_sampling_factor, Reference_frequency,
                               RRC_ROLL_OFF, DAC_Resolution_Bits, DAC_OUTPUT_VPP, Linewidth, output_power, Extinction_ratio, VPI, VDC, osnr)

    """ 接收端 """
    """ 光源 """
    LO_Linewidth = 1e3
    frequency_offset = 1.5e9
    LO_power = 17

    """" 相干接收机 """
    Responsivity = 1

    """ ADC """
    ADC_sampling_rate = 120e9
    ADC_resolution_bits = 8

    """ RRC """
    RRC_ROLL_OFF = 0.01

    """ 自适应均衡器 """
    num_tap = 25
    ref_power_cma = 2
    cma_convergence = 33000
    step_size_cma = 2e-9
    step_size_rde = 2e-9

    """ BPS """
    num_test_angle = 64
    block_size = 120

    signals = phot.receiver(signals, num_symbols, bits_per_symbol, total_baud, up_sampling_factor, Reference_frequency, LO_Linewidth, frequency_offset, LO_power,
                            Responsivity, ADC_sampling_rate, ADC_resolution_bits, RRC_ROLL_OFF, num_tap, ref_power_cma, cma_convergence, step_size_cma, step_size_rde, num_test_angle, block_size)

    # 分析器画星座图
    phot.constellation_diagram(signals, isdata = False)


    # """ 此处开始计算误码率 """
    # # 返回误码率和 Q 影响因子
    # ber, q_factor = phot.bits_error_count(
    #     signals, bits_per_symbol)

    # print(ber)
    # print(q_factor)
